#include <iostream>
#include <cstring>
using namespace std;

int const oo = 1<<27;
int const maxn = 1002;
int const maxw = 32;
int f[maxn][maxw][2];
int n, w;

int main()
{
	memset(f, -oo, sizeof(f));
	ios::sync_with_stdio(false);
	cin >> n >> w;
	int tmp; cin >> tmp;
	f[0][0][0] = int(tmp == 1);  f[0][1][1] = int(tmp == 2);
	for (int i = 1; i < n; i++) {
		cin >> tmp;
		f[i][0][0] = f[i-1][0][0] + int(tmp == 1);
		f[i][0][1] = f[i-1][0][1] + int(tmp == 2);
		for (int j = 1; j <= w; j++) {
			for (int k = 0; k < 2; k++) {
				int t = (tmp == k+1);
				f[i][j][k] = max(f[i-1][j][k] + t, f[i-1][j-1][1-k] + t);
			}
		}
	}
	int ans = 0;
	for (int i = 0; i <= w; i++) {
		if (f[n-1][i][0] > ans) ans = f[n-1][i][0];
		if (f[n-1][i][1] > ans) ans = f[n-1][i][1];
	}
	cout << ans << "\n";
}

